package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.Email;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class EmailDao extends BaseDao {
	// property constants
	public static final String EMAIL_ADDRESS = "emailAddress";
	public static final String EMAIL_POP3 = "emailPop3";
	public static final String EMAIL_PASSWORD = "emailPassword";
	public static final String USER_ID = "userId";

	public int delete(Email persistentInstance) {
		try {
			String sql = "delete from email where emailId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getEmailId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<Email> findAll() {
		try {
			String sql = "select * from email";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Email> findByEmailAddress(Object emailAddress) {
		return findByProperty(EMAIL_ADDRESS, emailAddress);
	}

	public List<Email> findByEmailPassword(Object emailPassword) {
		return findByProperty(EMAIL_PASSWORD, emailPassword);
	}

	public List<Email> findByEmailPop3(Object emailPop3) {
		return findByProperty(EMAIL_POP3, emailPop3);
	}

	public Email findById(java.lang.Integer id) {
		try {
			String sql = "select * from email where emailId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<Email> instanceList = this.getListByResultSet(ps
					.executeQuery());
			if (instanceList == null) {
				return null;
			}
			Email instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Email> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from email where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Email> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	@Override
	protected List<Email> getListByResultSet(ResultSet rs) {
		// TODO Auto-generated method stub
		List<Email> list = new ArrayList<Email>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				Email email = new Email();
				email.setEmailId(rs.getInt("emailId"));
				email.setEmailAddress(rs.getString("emailAddress"));
				email.setEmailPop3(rs.getString("emailPOP3"));
				email.setEmailPassword(rs.getString("emailPassword"));
				list.add(email);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(Email transientInstance) {
		try {
			String sql = "insert into email(emailAddress, emailPOP3, emailPassword, userId) values(?, ?, ?, ?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, transientInstance.getEmailAddress());
			ps.setString(2, transientInstance.getEmailPop3());
			ps.setString(3, transientInstance.getEmailPassword());
			ps.setInt(4, transientInstance.getUser().getUserId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {
		// TODO Auto-generated method stub

	}

	public int update(Email instance) {
		try {
			String sql = "update email set emailAddress=?, emailPOP3=?, emailPassword=? where emailId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, instance.getEmailAddress());
			ps.setString(2, instance.getEmailPop3());
			ps.setString(3, instance.getEmailPassword());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}